package io.renren.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.RedisUtils;
import io.renren.modules.sys.dao.SysUserDao;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.GetPasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author hsp
 * @Description TODO
 * @createTime 2020年09月21日 17:23:00
 */
@Service
public class GetPasswordServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> implements GetPasswordService {
    @Autowired
    private MailSender mailSender;
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public String verifyEmailAndUsername(Map<String, Object> params) {
        String email = (String) params.get("email");
        String username = (String) params.get("username");
        String state = "";
        QueryWrapper<SysUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email);
        queryWrapper.eq("username", username);
        List<SysUserEntity> list = baseMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            state = "100";
        } else {
            state = "000";
        }
        return state;
    }

    @Override
    public String sendEmail(Map<String, Object> params) {
        String email = (String) params.get("outEmail");
        //生成随机验证码
        String checkCode = String.valueOf(new Random().nextInt(899999) + 100000);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("15994054231@163.com");
        message.setTo(email);
        message.setSubject("这是一个邮件主题——系统邮件");
        message.setText("您正在修改您的密码，本次验证码为：" + checkCode + "\n 如非本人操作，请忽略！谢谢");
        mailSender.send(message);
        //System.out.println("邮件发送成功");
        redisUtils.set("checkCode", checkCode,3600);
        return checkCode;
    }

    @Override
    public String codeVerify(Map<String, Object> params) {
        String code = (String) params.get("checkCode");
        String state = "";
        String checkCode = redisUtils.get("checkCode");
        if (checkCode.isEmpty()){
            return "请开启缓存数据库";
        }
        if (checkCode.equals(code)) {
            state = "100";
        } else {
            state = "000";
        }
        return state;
    }

    @Override
    public String updatePwd( SysUserEntity updateEntity) {
        String message = "";
        QueryWrapper<SysUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", updateEntity.getUsername());
        SysUserEntity userEntity = baseMapper.selectOne(queryWrapper);
        if (userEntity==null){
            return "没有该用户";
        }
        userEntity.setPassword(updateEntity.getPassword());
        UpdateWrapper<SysUserEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("username", updateEntity.getUsername());
        int update = baseMapper.update(userEntity, updateWrapper);
        if (update > 0) {
            message = "100";
        } else {
            message = "000";
        }
        return message;
    }

}
